<?php
/**
 * Created by IntelliJ IDEA.
 * User: xingshi
 * Date: 16/8/28
 * Time: 下午1:13
 */

namespace App\Http\Controllers;

use App\Http\Requests;
use Auth;
use Cache;
use Cookie;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Input;
use Log;
use Mail;
use Redirect;
use Reponse;
use View;
use Crypt;
use App\Company;
use App\Job;
use Validator;
use App\Permission;
use App\Invitation;
use App\Info;
use App\User;
use App\Resume;
use App\Utils;
use App\Message;
use App\Userword;


class talentController extends Controller
{
    static $URL = '/';
    public function __construct()
    {
        //          $this->middleware('auth');
    }
	public function index(Request $request){
		$input = $request->all();
		if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
		else $userid2 = Utils::id2url((Auth::user()->id));
		$userid = Utils::url2id($userid2);
		if (!Permission::user_is_super() && $userid!=Auth::user()->id) return redirect('/home');
		if(!Permission::user_can_talent($userid)) return redirect('/home');

		$isself = false;
		if($userid==Auth::user()->id) $isself = true;

        $data = DB::select("select * from resumes where userid=".$userid);
		$isneed = 0;
		$realname = "";
		if(count($data)>0) {
			if($data[0]->realname!="") {
				$realname = $data[0]->realname;
			}			
			$isneed = $data[0]->isneed;
		}

		$user = User::find($userid);	
		$userword = Userword::where('userid','=',$userid)->orderby("id","desc")->get()->first();

        $data = DB::select("select count(*) as all_cnt from invitations where isdelete=0 and userid=".$userid." order by updated_at desc");
		$all_cnt = $data[0]->all_cnt;
		$all_not_read_data = DB::select("select count(*) as cnt from infos where isdelete=0 and receiverid=".Auth::user()->id." and isread=0");
		$all_not_read = $all_not_read_data[0]->cnt;

        $data = DB::select("select count(*) as not_cnt from invitations where status=1 and isdelete=0 and userid=".$userid." order by updated_at desc");
		$not_cnt = $data[0]->not_cnt;

        $data = DB::select("select count(*) as interview_cnt from invitations where (status=210 or status=211 or status=214) and isdelete=0 and userid=".$userid." order by updated_at desc");
		$interview_cnt = $data[0]->interview_cnt;
		$interview_not_read_data = DB::select("select count(*) as cnt from infos where invitid in (select id from invitations where (status=210 or status=211 or status=214) and isdelete=0 and userid=".$userid.")"." and receiverid=".Auth::user()->id." and isread=0");
		$interview_not_read = $interview_not_read_data[0]->cnt;

        $data = DB::select("select count(*) as result_cnt from invitations where (status=2121 or status=2120 or status=213 or status=2122) and isdelete=0 and userid=".$userid." order by updated_at desc");
		$result_cnt = $data[0]->result_cnt;
		$result_not_read_data = DB::select("select count(*) as cnt from infos where invitid in (select id from invitations where (status=2121 or status=2120 or status=213 or status=2122) and isdelete=0 and userid=".$userid.")"." and receiverid=".Auth::user()->id." and isread=0");
		$result_not_read = $result_not_read_data[0]->cnt;

        return View::make("talent.index")->with(compact("all_cnt"))->with(compact("not_cnt"))->with(compact("interview_cnt"))->with(compact("result_cnt"))->with(compact("userid2"))->with(compact("isself"))->with(compact("realname"))->with(compact("isneed"))->with(compact("all_not_read"))->with(compact("interview_not_read"))->with(compact("result_not_read"))->with(compact("user"))->with(compact("userword"));
    }

    public function invit(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

        $invits = DB::select("select * from invitations where isdelete=0 and userid=".$userid." order by updated_at desc");

		$alljobs = array();
		$alljobs['acc'] = array();
		$alljobs['notacc'] = array();
        foreach($invits as $k=>$v){
            $jobid = $v->jobid;
			$job = Job::find($jobid);
			$com = Company::find($job->companyid);

			$jjob = array();
			$jjob['title'] = $job->title;
			$jjob['salary'] = $job->salary;
			$jjob['company_avatar_url'] = $com->avatar_url;
			$jjob['company_name'] = $com->name;
			$jjob['invitid'] = $v->id;
			
			$not_read_data = DB::select("select count(*) as cnt from infos where isdelete=0 and invitid=".$v->id." and receiverid=".Auth::user()->id." and isread=0");
			$jjob['not_read'] = $not_read_data[0]->cnt;

			if($v->status==1) $alljobs['notacc'][] = $jjob;
			else {
				$str = "已接受";
				if($v->status == 20) $str = " 你已拒绝了该公司的面试邀请";
				$jjob['statusstr'] = $str;
				$alljobs['acc'][] = $jjob; 
			}
        }
        return response()->json($alljobs);
    }

    public function interview(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

        $invits = DB::select("select * from invitations where status>=21 and isdelete=0 and userid=".$userid." order by updated_at desc");

		$alljobs = array();
		$alljobs['before'] = array();
		$alljobs['in'] = array();
		$alljobs['after'] = array();
        foreach($invits as $k=>$v){
            $jobid = $v->jobid;
			$job = Job::find($jobid);
			$com = Company::find($job->companyid);

			$jjob = array();
			$jjob['title'] = $job->title;
			$jjob['salary'] = $job->salary;
			$jjob['company_avatar_url'] = $com->avatar_url;
			$jjob['company_name'] = $com->name;
			$jjob['invitid'] = $v->id;

			$not_read_data = DB::select("select count(*) as cnt from infos where isdelete=0 and invitid=".$v->id." and receiverid=".Auth::user()->id." and isread=0");
			$jjob['not_read'] = $not_read_data[0]->cnt;

			$str = "";
			if($v->status==210) {
				$jjob['statusstr'] = "已接受邀请, 时间地点待定";
				$alljobs['before'][] = $jjob;
			}
			else if($v->status==211){
				$jjob['statusstr'] = "时间地点已定, 待面试";
				$alljobs['in'][] = $jjob; 
			}
			else if($v->status==214){
				$jjob['statusstr'] = "已面试, 等公司反馈";
				$alljobs['after'][] = $jjob; 
			}
        }
//var_dump($alljobs);
        return response()->json($alljobs);

    }

    public function result(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

        $invits = DB::select("select * from invitations where status>=212 and isdelete=0 and userid=".$userid." order by updated_at desc");

		$alljobs = array();
		$alljobs['suit'] = array();
		$alljobs['notsuit'] = array();
		$alljobs['in'] = array();
        foreach($invits as $k=>$v){
            $jobid = $v->jobid;
			$job = Job::find($jobid);
			$com = Company::find($job->companyid);

			$jjob = array();
			$jjob['title'] = $job->title;
			$jjob['salary'] = $job->salary;
			$jjob['company_avatar_url'] = $com->avatar_url;
			$jjob['company_name'] = $com->name;
			$jjob['invitid'] = $v->id;

			$not_read_data = DB::select("select count(*) as cnt from infos where isdelete=0 and invitid=".$v->id." and receiverid=".Auth::user()->id." and isread=0");
			$jjob['not_read'] = $not_read_data[0]->cnt;

			$str = "";
			$status = $v->status;
			if($status==2121) {
				$jjob['statusstr'] = "面试者已接受offer";
				$alljobs['suit'][] = $jjob;
			}
			else if($status==2120){
				$jjob['statusstr'] = "已发offer, 待面试者接受";
				$alljobs['in'][] = $jjob; 
			}
			else if($status==213 or $status==2122){
				if($status==213) $jjob['statusstr'] = "已面试, 公司拒绝面试者";
				else $jjob['statusstr'] = "已发offer, 面试者拒绝";
				$alljobs['notsuit'][] = $jjob; 
			}
        }
        return response()->json($alljobs);

    }

    public function invitdata(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

		$invitid = $input["invitid"];

		$invit = Invitation::find($invitid);
		$jobid = $invit->jobid;
		$job = Job::find($jobid);
        $company = Company::find($job->companyid);
		
		$jobdata = array();
		$jobdata['title'] = $job->title;
		$jobdata['salary'] = $job->salary;
		$jobdata['desc'] = $job->desc;
		$jobdata['invitid'] = $invit->id;
		$jobdata['company_avatar_url'] = $company->avatar_url;
		$jobdata['company_name'] = $company->name;
		$jobdata['company_industry'] = $company->industry;
		$jobdata['company_size'] = $company->size;
		$jobdata['company_foundation'] = $company->foundation;
		$jobdata['company_location'] = $company->location;
		$jobdata['company_introduction'] = $company->introduction;
		$jobdata['company_tags'] = json_decode($company->tags);
		$jobdata['statusstr'] = "已处理";
		$jobdata['status'] = $invit->status;
		$jobdata['isself'] = $isself;
		if($invit->status==1) $jobdata['statusstr'] = "已邀请, 待候选人处理";

        //$userid = Auth::user()->id;
        $messages = DB::select("select id,senderid,receiverid,content,created_at,status,isdone from infos where (senderid=".$userid." or receiverid=".$userid.") and invitid=".$invitid." order by updated_at asc");

		$qluser = User::find(1308);
		foreach($messages as $k=>$v){
			if($v->senderid == 1308) {
				$v->name = $qluser->name;
				$v->avatar_file_name = $qluser->avatar->url('thumb');
			}
			else{
				$user = User::find($userid);
				$v->name = $user->name;
				$v->avatar_file_name = $user->avatar->url('thumb');
			}
		}

		$data = array();
		$data['jobdata'] = $jobdata;
		$data['messages'] = $messages;

		DB::update("update infos set isread=1 where receiverid=".Auth::user()->id." and invitid=".$invit->id);	

        return response()->json($data);
    }
	
	public function save_invit($infoid, $userid, $invitid, $status, $content){
		$invit = Invitation::find($invitid);
		$invit->status = $status;
		$invit->save();
		
		$info = Info::find($infoid);
		$info->isdone = 1;
		$info->save();
		$data = self::create_info($userid, 1308, $invitid, $content, $status);
	}
	// 接受用户修改邀请状态
	public function acc(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;
		if(!$isself) return false;

		$invitid = $input["invitid"];
		$status = $input["status"];
		$infoid = $input["infoid"];

		$content = "";
		if($status==20) $content = "候选人已拒绝邀请";
		else if($status==210) $content = "已接受邀请, 时间地点待定";
		else if($status==2121) $content = "面试者已接受offer";
		else if($status==2122) $content = "已发offer, 面试者拒绝";

		self::save_invit($infoid, $userid, $invitid, $status, $content);


        $data = DB::select("select count(*) as all_cnt from invitations where isdelete=0 and userid=".$userid." order by updated_at desc");
		$all_cnt = $data[0]->all_cnt;

        $data = DB::select("select count(*) as not_cnt from invitations where status=1 and isdelete=0 and userid=".$userid." order by updated_at desc");
		$not_cnt = $data[0]->not_cnt;

        $data = DB::select("select count(*) as interview_cnt from invitations where (status=210 or status=211 or status=214) and isdelete=0 and userid=".$userid." order by updated_at desc");
		$interview_cnt = $data[0]->interview_cnt;

		$result = array();
		$result['acc'] = true;
		$result['all_cnt'] = $all_cnt;
		$result['not_cnt'] = $not_cnt;
		$result['interview_cnt'] = $interview_cnt;
        return response()->json($result);

	}

	public function create_info($senderid, $receiverid, $invitid, $content, $status=-1){
        $data = array();
        $data['senderid'] = $senderid;
        $data['receiverid'] = $receiverid;
        $data['invitid'] = $invitid;
        $data['content'] = $content;
        $data['status'] = $status;
        $data['isdelete'] = 0;

        return Info::create($data);
	}

	public function info(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;
		//if(!$isself) return false;

		$senderid = $input['senderid'];
		$receiverid = $input['receiverid'];
		
		$info = self::create_info($senderid, $receiverid, $input['invitid'], $input['content']);

		$user = User::find($info->senderid);
		$data = array();
		$data['id'] = $info->id;
		$data['senderid'] = $info->senderid;
		$data['receiverid'] = $info->receiverid;
		$data['content'] = $info->content;
		$data['created_at'] = substr($info->created_at,0,19);
		$data['status'] = $info->status;
		$data['isdone'] = $info->isdone;
		$data['name'] = $user->name;
		$data['avatar_file_name'] = $user->avatar->url('thumb');

        $result = array();
        $result['acc'] = true;
        $result['data'] = $data;
        return response()->json($result);
    }

    public function getinfo(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

        $invitid = $input["invitid"];
        $data = DB::select("select senderid,receiverid,content,created_at,status from infos where (senderid=".$userid." or receiverid=".$userid.") and invitid=".$invitid." order by updated_at desc");

        return response()->json($data);

    }

	public function commit_invit(Request $request){
		$input = $request->all();
        if(array_key_exists('userid', $input)) $userid2 = $input['userid'];
        else $userid2 = Utils::id2url((Auth::user()->id));
        $userid = Utils::url2id($userid2);
        if (!Permission::user_is_super() && $userid!=Auth::user()->id) return;

        $isself = false;
        if($userid==Auth::user()->id) $isself = true;

		if(!$isself) return false;

		$resume = Resume::where('userid','=',$userid)->get()->first();
		if(count($resume) == 0){ 
			$da = array();
            $da['userid'] = $userid;
			$da['isneed'] = 1;
            Resume::create($da);
        }   
        else {
			$resume->isneed = 1;
            $resume->save();
        }

		$result = array();
		$result['acc'] = true;
		return redirect()->action('talentController@index');
		
	}

//admin 后台页面

	public function admin(Request $request){
		if (!Permission::user_is_super()) return;

		$data = array();

		$resumes = Resume::where('isneed','=','1')->get();
		foreach($resumes as $resume){
			$da = array();
			$da['userid2'] = Utils::id2url($resume->userid); 
			$da['invitid'] = -1;
			$da['name'] = $resume->realname;
			$da['create_at'] = $resume->create_at;
			$da['company_name'] = "";
			$da['title'] = "";
			$da['salary'] = "";
			$da['status'] = -2;
			$da['statusstr'] = "已申请, 待邀请";

			$data[] = $da;
		}

        $invits = DB::select("select * from invitations where isdelete=0 order by updated_at asc");
		foreach($invits as $k=>$v){
			$da = array();

			$userid = $v->userid;
			$jobid = $v->jobid;
			$status = $v->status;

			$resume = Resume::where('userid','=',$userid)->get()->first();
			$job = Job::find($jobid);
			$com = Company::find($job->companyid);

			$da['userid2'] = Utils::id2url($userid);
			$da['invitid'] = $v->id;
			$da['name'] = $resume->realname;
			$da['create_at'] = $v->created_at;
			$da['company_name'] = $com->name;
			$da['title'] = $job->title;
			$da['salary'] = $job->salary;
			$da['status'] = $status;
			$da['statusstr'] = "已申请, 待邀请";
			if($status==1) $da['statusstr'] = "已邀请, 待候选人处理";
			if($status==20) $da['statusstr'] = "候选人已拒绝邀请";
			if($status==210) $da['statusstr'] = "已接受邀请, 时间地点待定";
			if($status==211) $da['statusstr'] = "时间地点已定, 待面试";
			if($status==2120) $da['statusstr'] = "已发offer, 待面试者接受";
			if($status==2121) $da['statusstr'] = "面试者已接受offer";
			if($status==2122) $da['statusstr'] = "已发offer, 面试者拒绝";
			if($status==213) $da['statusstr'] = "已面试, 公司拒绝面试者";
			if($status==214) $da['statusstr'] = "面试结束, 等公司反馈";

			$data[] = $da;
		}

		return View::make("talent.admin")->with(compact("data"));
	}

	public function search_com(Request $request){
		if (!Permission::user_is_super()) return;
		$input = $request->all();
		$name = $input['name'];
		if(strlen($name)>0){
			$coms = DB::select("select id,name from companies where name like '%".$name."%'");
			$result = array();
			$result['acc'] = true;
			$result['data'] = $coms;
			return response()->json($result);
		}
	}

	public function send_invit(Request $request){
		if (!Permission::user_is_super()) return;
		$input = $request->all();
		$invitid = $input['invitid'];
		$userid = Utils::url2id($input['userid']);

		$job = array();
		$job['companyid'] = $input['companyid'];
		$job['title'] = $input['title'];
		$job['salary'] = $input['salary'];
		$job['desc'] = $input['desc'];
		$data = Job::create($job);
		$jobid = $data->id;

		$invit = array();
		$invit['userid'] = $userid;
		$invit['jobid'] = $jobid;
		$invit['status'] = 1;
		$invitdata = Invitation::create($invit);

		$info = array();
		$info['senderid'] = 1308;
		$info['receiverid'] = $userid;
		$info['content'] = "已邀请, 待候选人处理";
		$info['status'] = 1;
		$info['invitid'] = $invitdata->id;
		$info['isdone'] = 0;
		Info::create($info);

		return redirect()->action('talentController@admin');
	}

	public function send_invittime(Request $request){
		if (!Permission::user_is_super()) return;
		$input = $request->all();
		$invitid = $input['invitid'];

		$invit = Invitation::find($invitid);
		$invit->interview_time = $input['invittime'];
		$invit->interview_address = $input['invitaddress'];
		$invit->remark = $input['remark'];
		$invit->status = 211;
		$invit->save();

		$info = array();
		$info['senderid'] = 1308;
		$info['receiverid'] = $invit->userid;
		$info['content'] = "面试时间地点已定，请按时面试。 \n 时间：".$input['invittime']."\n地点：".$input['invitaddress'];
		$info['invitid'] = $invitid;
		$info['status'] = 211;
		$info['isdone'] = 0;
		Info::create($info);
		
		return redirect()->action('talentController@admin');
	}

	public function send_offer(Request $request){
		if (!Permission::user_is_super()) return;
		$input = $request->all();
		$invitid = $input['invitid'];

		$invit = Invitation::find($invitid);
		$invit->status = 2120;
		$invit->final_title = $input['final_title'];
		$invit->final_salary = $input['final_salary'];
		$invit->remark = $input['remark'];
		$invit->save();
		
		$info = array();
		$info['senderid'] = 1308;
		$info['receiverid'] = $invit->userid;
		$info['content'] = "已发offer, 待面试者接受\n 职位：".$input['final_title']."\n薪资：".$input['final_salary'];
		$info['invitid'] = $invitid;
		$info['status'] = 2120;
		$info['isdone'] = 0;
		Info::create($info);
			
		return redirect()->action('talentController@admin');
	}

	public function notpass_offer(Request $request){
		if (!Permission::user_is_super()) return;
		$input = $request->all();
		$invitid = $input['invitid'];

		$invit = Invitation::find($invitid);
		$invit->status = 213;
		$invit->remark = $input['remark'];
		$invit->save();

		$info = array();
		$info['senderid'] = 1308;
		$info['receiverid'] = $invit->userid;
		$info['content'] = "已面试, 公司拒绝面试者";
		$info['invitid'] = $invitid;
		$info['status'] = 213;
		$info['isdone'] = 0;
		Info::create($info);

		return redirect()->action('talentController@admin');
	}

	public function end_invit(Request $request){
		if (!Permission::user_is_super()) return;
		$input = $request->all();
		$invitid = $input['invitid'];

		$invit = Invitation::find($invitid);
		$invit->status = 214;
		$invit->save();

		$info = array();
		$info['senderid'] = 1308;
		$info['receiverid'] = $invit->userid;
		$info['content'] = "已面试, 等公司反馈";
		$info['invitid'] = $invitid;
		$info['status'] = 214;
		$info['isdone'] = 0;
		Info::create($info);

		return redirect()->action('talentController@admin');
		
	}
	public function create(Request $request)
	{
		return View::make("talent.create");
	}
	public function test(Request $request)
	{
		if ($request->isMethod('post'))
		{
		$input=$request->all();
		$m=new Message();
		$userid=$input['userid'];
		$user=User::find($userid);
		$user->msgcnt+=1;
		$user->offer=1;
		$user->save();
		$m->sendMail(1,$userid,"恭喜你成功解锁了隐藏关卡，点击 <a href='http://www.qlcoder.com/talent'>人才服务</a> 查看详情。");
		$content=$input['content'];
		$data=array();
		$data['userid']=$userid;
		$data['words']=$content;
		Userword::create($data);

		var_dump("shiniantest");
		}
	}

}
